package com.bw.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.bw.domain.SysMenu;
import com.bw.service.SysMenuService;
import com.bw.utils.common.JWTUtils;
import com.bw.utils.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("menu")
@RestController
public class MenuController {
    @Autowired
    SysMenuService sysMenuService;

    @RequestMapping("list")
    public Result result(){
        String userId = JWTUtils.getUserId();
        List<SysMenu> menuList = sysMenuService.listByUserId(userId);
//配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(menuList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("path",treeNode.getPath());
                    tree.putExtra("component", treeNode.getComponent());
                });
                return Result.success(treeNodes);
    }
}
